import { getUserInfo } from "@/api/user";
import { User } from "@/api/user/type";
import { defineStore } from "pinia";
import { computed, ref } from "vue";

export const useUserStore = defineStore('user', () => {
    const user = ref({} as User);
    const getUser = computed(() => user.value)
    const getId = computed(() => user.value.id)
    const getRoleCode = computed(() => user.value.roleCode)
    async function doGetUserInfo() {
        if (Object.keys(user.value).length == 0) {
            const res = await getUserInfo();
            if (res !== undefined) {
                user.value = res.data as User;
            }
        }
        return user.value;
    }
    return {
        user,
        getUser, getId, getRoleCode,
        doGetUserInfo
    }
})